﻿using System;
using System.Web;
using Amba.NotFoundLog.Models;
using Orchard;
using Orchard.Data;

namespace Amba.NotFoundLog.Services
{
    public interface INotFoundLogService  : IDependency
    {
        void LogNotFound(HttpRequest request);
        void LogNotFound(string url, string referer, HttpRequest request = null);
    }

    public class NotFoundLogService : INotFoundLogService
    {
        private IRepository<NotFoundLogRecord> _repository;

        public NotFoundLogService(IRepository<NotFoundLogRecord> repository)
        {
            _repository = repository;
        }
 
        public void LogNotFound(HttpRequest request)
        {
            LogNotFound(
                request.RawUrl,
                request.UrlReferrer != null ? request.UrlReferrer.AbsolutePath : null,
                request
                );
        }

        public void LogNotFound(string url, string referer, HttpRequest request = null)
        {
            _repository.Create(new NotFoundLogRecord()
            {
                LogDate = DateTime.UtcNow,
                Url = url,
                Referer = referer
            });
        }
    }
}